<?php

if (!defined('DIR_SEP')) {
    define('DIR_SEP', DIRECTORY_SEPARATOR);
}

if (!defined('MODULES_DIR')) {
    define('MODULES_DIR', dirname(__FILE__).DIR_SEP."..".DIR_SEP);
}

require_once(MODULES_DIR.'db.inc');
require_once(MODULES_DIR.'util.inc');
require_once(MODULES_DIR.'/blog/blogpost.inc');

class BlogBox
{
	function __construct()
	{
		$this->db = DB::getInstance()->getDB();
	}

	public static function get5Latest()
	{
		try
		{
			global $G_HOME;
			$result = '';
			$postItems = BlogPost::getPosts("tbp.creation_date desc",0,5);
			
			if(is_null($postItems))
				return null;
				
			foreach($postItems as $postItem)
			{
				$postText = Util::cleanTags($postItem->content);
					
				$postText = substr($postText,0,min(strlen($postText),170))."...";
				
				$feedContent = 
				$result .= '<div class="boxItem">';
				$result .= "<h2 class=\"boxTitle\"><a href='".$postItem->getUrl()."'>".$postItem->title."</a></h2>";
				$result .= "<ul>";
				$result .= '<li class="boxDate">'.date('d/m/Y',$postItem->creationDate).'</li>';
				$result .= '<li class="boxClicks">'.$postItem->viewCount.'</li>';
				$result .= '<li class="boxComments">'.$postItem->commentCount.'</li>';
				$result .= "</ul>";
				$result .= '<div class="readableText">'.$postText.'</div>';
				$result .= '</div>';
			}
						
			return $result;
		}
		catch(Exception $ex)
		{
			return null;
		}
	}
}